const Controllers = require('../controllers/controllers.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:'',
    nickname:'',
    sex:1,
    portrait:'',
    mobile:'',
    timer: '', //定时器名字
    countDownNum: '60', //倒计时初始值
    codeDis: false,
    telcode: '发送验证码',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    uuid:'',
    parentid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(wx.getStorageSync('userInfo'))
    if(app.globalData.parentid){
      this.setData({
        parentid:app.globalData.parentid
      })
    }
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  onReady: function() {
    if (app.globalData.userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: app.globalData.userInfo
      })

    } else if (this.data.canIUse) {
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          hasUserInfo: true,
          userInfo: app.globalData.userInfo
        })

      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          });
          wx.setStorageSync('userInfo', res.userInfo)
        }
      })
    }
  },
  onShow: function () {
    this.gohome()
  },
  gohome() {
    
    let mobile = wx.getStorageSync('mobile');
    if (mobile) {
      wx.switchTab({
        url: '/pages/Index/Index'
      });
    }
      
  },
  //申请授权判断
  getUserInfo(e) {
    if (e.detail.userInfo) {
      this.setData({
        hasUserInfo: true
      })
      wx.setStorageSync('userInfo', e.detail.userInfo)
      app.globalData.userInfo = e.detail.userInfo
      let userInfo = e.detail.userInfo;
      this.setData({
        portrait:userInfo.avatarUrl,
        sex:userInfo.gender,
        nickname:userInfo.nickName,
      })
      
    } else {
      wx.showModal({
        title: '提示',
        content: '授权登录，方可登录注册',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      
    }
  },
  countDown: function() {
    let that = this;
    let countDownNum = that.data.countDownNum;
    that.setData({
      timer: setInterval(function() {
        countDownNum--;
        that.setData({
          countDownNum: countDownNum,
          codeDis: true
        })
        if (countDownNum <= 0) {
          that.setData({
            codeDis: false,
            countDownNum: 60
          })
          clearInterval(that.data.timer);
        }
      }, 1000)
    })
  },
  //发送短信验证码
  sendCode: function() {
    this.gohome()
    if (this.data.mobile == '') {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!(/^1[123456789]\d{9}$/.test(this.data.mobile))) {
      wx.showToast({
        title: '手机号码错误',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    // 获取数据
    Controllers.sendBindCode({
      mobile: this.data.mobile
    }).then(res => {
      this.setData({
        uuid:res.data.baseData.uuid
      })
      wx.showToast({
        title: '发送成功！',
        icon: 'success',
        duration: 1500
      })
      this.countDown();
    });
  },
  changeinput(e){
    let data = {};
    data[e.currentTarget.dataset.name]=e.detail.value
    this.setData(data)
  },
  formSubmit(e) {
    this.gohome()
    if (this.data.mobile == '') {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!(/^1[123456789]\d{9}$/.test(this.data.mobile))) {
      wx.showToast({
        title: '手机号码错误',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (this.data.code == '') {
        wx.showToast({
          title: '请输入验证码',
          icon: 'none',
          duration: 1500
        })
        return false;
    }
    

    let data={
      mobile:this.data.mobile,
      code:this.data.code,
      uuid:this.data.uuid,
      parentId:this.data.parentid
    }

    Controllers.bindMobile(data).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 1500
        })
        wx.setStorageSync('mobile', res.data.mobile)
        setTimeout(res=>{
          wx.switchTab({
            url: '/pages/Index/Index'
          })
        },1500)
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    }).catch(res=>{
      
    })
  },
  getPhoneNumber(e) {
    this.gohome()
    let data={
      nickname:this.data.nickname  || wx.getStorageSync('userInfo').nickName,
      encrypted_data:e.detail.encryptedData,
      iv:e.detail.iv,
      portrait:this.data.portrait  || wx.getStorageSync('userInfo').avatarUrl,
      sex:this.data.sex || wx.getStorageSync('userInfo').gender
    }
    Controllers.autoBind(data).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 1500
        })
        wx.setStorageSync('mobile', res.data.mobile)
        var that=this;
        setTimeout(res => {
          wx.switchTab({
            url: '/pages/Index/Index'
          })
          // that.interest()
        },1500)
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    }).catch(res=>{
      
    })
  },
  interest(){
    Controller.checkCollectInfo().then(res=>{
      if(res.data.baseData==1){
          wx.switchTab({
            url: '/pages/Index/Index'
          })
      }else{
        wx.navigateTo({
          url: "/pages/interest/interest"
        })
      }
      
    })
  },



})